﻿using Nop.Core;
using Nop.Core.Domain.Catalog;
using Nop.Core.Domain.Customers;
using Nop.Core.Domain.Orders;
using Nop.Services.Catalog;
using Nop.Services.Orders;
using System.Collections.Generic;
using System.Linq;

namespace Nop.Plugin.Misc.Reservations.Services
{
    public class ReservationWorkContext : IReservationWorkContext
    {
        private readonly IProductService _productService;
        private readonly IShoppingCartService _shoppingCartService;
        private readonly IWorkContext _workContext;
        private readonly IStoreContext _storeContext;

        public ReservationWorkContext(IProductService productService, 
            IShoppingCartService shoppingCartService,
            IWorkContext workContext, IStoreContext storeContext)
        {
            _productService = productService;
            _shoppingCartService = shoppingCartService;
            _workContext = workContext;
            _storeContext = storeContext;
        }

        public Customer CurrentCustomer
        {
            get { return _workContext.CurrentCustomer; }
        }

        public bool AddItemToCart(int pid, int quantity)
        {
            Product product = _productService.GetProductById(pid);
            if (product != null && product.ProductVariants.Count == 1)
            {
                ProductVariant productVariant = product.ProductVariants.FirstOrDefault();
                IList<ShoppingCartItem> cart = Cart();
                ShoppingCartItem shoppingCartItem = FindItemInShoppingCart(cart, productVariant);
                AddItemToCart(shoppingCartItem, productVariant, quantity);

                return true;
            }
            return false;
        }

        private IList<ShoppingCartItem> Cart()
        {
            return CurrentCustomer.ShoppingCartItems
                .Where(sci => sci.ShoppingCartType == ShoppingCartType.ShoppingCart)
                .Where(sci => sci.StoreId == _storeContext.CurrentStore.Id)
                .ToList();
        }

        private ShoppingCartItem FindItemInShoppingCart(IList<ShoppingCartItem> cart, ProductVariant productVariant)
        {
            return _shoppingCartService.FindShoppingCartItemInTheCart(cart, ShoppingCartType.ShoppingCart, productVariant);
        }

        private void AddItemToCart(ShoppingCartItem item, ProductVariant productVariant, int quantity)
        {
            if (item == null)
            {
                int currentStoreId = _storeContext.CurrentStore.Id;
                _shoppingCartService.AddToCart(
                    CurrentCustomer,
                    productVariant,
                    ShoppingCartType.ShoppingCart,
                    currentStoreId,
                    "",
                    decimal.Zero,
                    quantity,
                    true);
            }
            else
            {
                _shoppingCartService.UpdateShoppingCartItem(
                    CurrentCustomer, 
                    item.Id, 
                    quantity, 
                    true);
            }
        }
    }
}
